/**
 * 公共脚本方法
 */
import {getFormat, removeCookies} from "./index";
import {ElMessageBox, ElNotification} from "element-plus";


/**
 * 清除缓存数据
 */
export const clearSessionStorage = () => {
    removeCookies();
    sessionStorage.removeItem('baseInfo');
}

export const notify = (message, type='success') => {
    ElNotification({
        title: '提示',
        duration: 1000,
        message,
        type
    })
};

export const messageBox = ElMessageBox;

export const toStockDetail = (router, stockCode, stockName) => {
    // router.push({
    //     path: '/stock-detail',
    //     query: {
    //         stockCode
    //     }
    // })

    window.open(`/#/stock-detail?stockCode=${stockCode}&stockName=${stockName}`)
}

export const openDf = (code) => {
    let stockCode = ''
    if (code === '000000') {
        stockCode = 'zs000001'
    } else {
        if (code.startsWith('6')) {
            stockCode = 'sh' + code
        } else {
            stockCode = 'sz' + code
        }
    }
    window.open(`https://quote.eastmoney.com/${stockCode}.html`)
}


// 定时刷新
export const timeRefresh = (cb) => {
    let hours = new Date().getHours()
    let day = new Date().getDay()

    if (((hours >= 9 && hours < 15) || hours >= 21) && !(day === 0 || day === 6)) {
        cb()
    }
}

// 进入全屏
function requestFullScreen(element = document.documentElement) {
    if (element.requestFullscreen) {
        element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    } else if (element.webkitRequestFullscreen) {
        element.webkitRequestFullscreen();
    } else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
    }
}

// 退出全屏
function exitFullscreen() {
    if (document.exitFullscreen) {
        document.exitFullscreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
        document.webkitExitFullscreen();
    }
}


// 全屏
export const toggleFullScreen = () => {
    if (!document.fullscreenElement) {
        // 进入全屏
        requestFullScreen()
    } else {
        // 退出全屏
       exitFullscreen()
    }
}



// 过滤st 退市 科 创业
export const filterStockList = (list=[]) => {
    return list.filter(item => {
        if(item.stockCode.startsWith('60') || item.stockCode.startsWith('00')) {
            if(item.stockName.indexOf('*ST') !== -1 || item.stockName.indexOf('ST') !== -1 || item.stockName.indexOf('退') !== -1) {
                return false
            } else return item.price >= 2;
        } else {
            return false
        }
    })
}

export const getColorName = (ratio) => {
    if(ratio > 0) {
        return 'danger';
    } else if(ratio < 0) {
        return 'success';
    } else {
        return 'info';
    }
}

export const getPriceColorName = (ratio, type) => {
    if(type === 'up') {
        if(ratio > 0) {
            return 'danger';
        } else {
            return 'info';
        }
    } else {
        if(ratio < 0) {
            return 'success';
        } else {
            return 'info';
        }
    }

}

export const getFuturesParams = (code, type) => {
    let num = code.replace(/[a-z,A-Z]/g, '')
    let str = code.replace(/[0-9]/g, '')
    if(num.length === 3) {
        let arr =code.split('')
        arr.splice(2,0,'2')
        code =arr.join('')
    }
    if(code.length <= 3){
        code = code.split('').slice(0, code.length -1).join('')
        code += '0'
    }
    let obj = {
        symbol: code,
        type: type,
        path: `${code}_${type}_${new Date().getTime()}=/InnerFuturesNewService.getFewMinLine`
    }

    if(type === '101'){
        obj = {
            symbol: code,
            _: '2024_9_5',
        }
        obj.path = obj.symbol + obj._ + '=/InnerFuturesNewService.getDailyKLine'
    }

    return obj
}